#include "CircleComponent.h"
#include "MainPanel.h"

CircleComponent::CircleComponent(MainPanel* parent, int id, int x, int y, int radius, String name,
		String filename) {
	this->id = id;
	this->x = x;
	this->y = y;
	this->radius = radius - 2 * outerBuffer;
	this->parent = parent;
	this->name = name;
	this->filename = filename;

	radians = 0;

	greyGradient = new GradientBrush(Colours::white, radius / 2, radius / 2, Colour(210, 210, 255), 0,
			0, true);
	orangeGradient = new GradientBrush(Colour(240, 200, 80), radius / 2, radius / 2, Colour(250, 240, 240), 0, 0,
			true);

	File iconFile(filename);
	image = PNGImageFormat().loadFrom(filename);
	if (image == 0) {
		std::cerr << "Could not load file " << filename.toUTF8() << std::endl;
	}
}

CircleComponent::~CircleComponent() {
}

void CircleComponent::mouseEnter(const MouseEvent& e) {
	setMouseCursor(MouseCursor::PointingHandCursor);
	state.mouseOver = true;
	startCircleAnimation();
	parent->setTextbox(name);
	repaint();
}

void CircleComponent::mouseExit(const MouseEvent& e) {
	state.mouseOver = false;
	parent->setTextbox("");
	stopCircleAnimation();
	setMouseCursor(MouseCursor::NormalCursor);
	repaint();
}

void CircleComponent::mouseDown(const MouseEvent& e) {
	repaint();
}

void CircleComponent::mouseUp(const MouseEvent& e) {
	parent->circleClicked(id);
}

void CircleComponent::paint(Graphics& g) {
	float offset = 5 * sin(radians);
	float offset2 = offset / 2;
	int inout = outerBuffer + innerBuffer;
	int twoIn = 2 * innerBuffer;

	g.setColour(Colours::lightgrey);
	g.fillEllipse(outerBuffer - offset2, outerBuffer - offset2, radius + offset, radius + offset);
	g.setBrush(state.mouseOver ? orangeGradient : greyGradient);
	g.fillEllipse(inout, inout, radius - twoIn, radius - twoIn);

	g.setColour(Colours::darkgrey);
	g.drawEllipse(outerBuffer - offset2, outerBuffer - offset2, radius + offset, radius + offset, 2);
	g.setColour(Colours::grey);
	g.drawEllipse(inout, inout, radius - twoIn, radius - twoIn, 1);
	g.drawImage(image, inout, inout, radius - twoIn, radius - twoIn, 0, 0, image->getWidth(),
			image->getHeight());
}

void CircleComponent::startCircleAnimation() {
	parent->animateCircle(id);
	parent->startTimer(20);
}

void CircleComponent::stopCircleAnimation() {
	parent->stopTimer();
	radians = 0;
}

void CircleComponent::incrementAngle() {
	radians += 0.4;
	repaint();
}
